package com.wn.icloud.rest;

import com.wn.icloud.biz.BaseBiz;
import com.wn.icloud.common.context.BaseContextHandler;
import com.wn.icloud.common.exception.BaseException;
import com.wn.icloud.common.exception.EnterpriseInfo.EnterpriseInfoException;
import com.wn.icloud.common.msg.ObjectRestCode;
import com.wn.icloud.common.msg.ObjectRestResponse;
import com.wn.icloud.common.msg.TableResultResponse;
import com.wn.icloud.common.util.Query;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author Rainer
 * @create 2017-06-15 8:48
 */
@Slf4j
//@ControllerAdvice
public class BaseController<Biz extends BaseBiz, Entity> {
    @Autowired
    protected HttpServletRequest request;
    @Autowired
    protected Biz baseBiz;
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse<Entity> add(@RequestBody Entity entity) {
        baseBiz.insertSelective(entity);
        return new ObjectRestResponse<Entity>();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<Entity> get(@PathVariable int id) {
        ObjectRestResponse<Entity> entityObjectRestResponse = new ObjectRestResponse<>();
        Object o = baseBiz.selectById(id);
        entityObjectRestResponse.data((Entity) o);
        return entityObjectRestResponse;
    }
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public ObjectRestResponse<Entity> update(@RequestBody Entity entity) {
        baseBiz.updateSelectiveById(entity);
        return new ObjectRestResponse<Entity>();
    }
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ObjectRestResponse<Entity> remove(@PathVariable int id) {
        baseBiz.deleteById(id);
        return new ObjectRestResponse<Entity>();
    }

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ResponseBody
    public List<Entity> all() {
        return baseBiz.selectListAll();
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public TableResultResponse<Entity> list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        return baseBiz.selectByQuery(query);
    }

    public String getCurrentUserName() {
        return BaseContextHandler.getUsername();
    }

    public String getCurrentUserId() {
        return BaseContextHandler.getUserID();
    }

    public <T> T checkNotNull(T reference) throws BaseException {
        if (reference == null) {
            throw new BaseException("Requested item wasn't found!");
        }
        return reference;
    }

    public <T> T checkNotNull(Optional<T> reference) throws BaseException {
        if (reference.isPresent()) {
            return reference.get();
        } else {
            throw new BaseException("Requested item wasn't found!");
        }
    }

    public void checkParameter(String name, String param) throws BaseException {
        if (StringUtils.isEmpty(param)) {
            throw new BaseException("Parameter '" + name + "' can't be empty!");
        }
    }

    public void checkArrayParameter(String name, String[] params) throws BaseException {
        if (params == null || params.length == 0) {
            throw new BaseException("Parameter '" + name + "' can't be empty!");
        } else {
            for (String param : params) {
                checkParameter(name, param);
            }
        }
    }


//    @ExceptionHandler(value = BaseException.class)
//    @ResponseBody
//    public ObjectRestResponse handleException (Exception e) {
//
//        //判断异常是否是我们定义的异常
//        if (e instanceof EnterpriseInfoException) {
//            return ObjectRestResponse.failure(ObjectRestCode.ENTERPRISE_INFO_ERROR);
//        } else {
//            log.error("【系统异常】{}", e);
//            return ObjectRestResponse.failure(ObjectRestCode.UNKNOWN_ERROR);
//        }
//    }


}
